package common.post.service;

import java.util.Date;
import java.util.List;

import common.customer.bean.Customer;
import common.post.bean.SInfo;
import common.post.bean.SInfoComment;
import common.post.dao.SInfoCommentDao;
import common.post.dao.SInfoDao;

public class SInfoCommentServiceImpl implements SInfoCommentService {
	private SInfoCommentDao sInfoCommentDao;
	private SInfoDao sInfoDao;
	@Override
	public boolean addSInfoCommentReply(long commentid, String replyContent,
			Customer user) {
		SInfoComment parent = this.sInfoCommentDao.get(commentid);
		
		SInfoComment s = new SInfoComment();
		s.setContent(replyContent);
		s.setPublishTime(new Date());
		s.setCustomer(user);
		s.setParent(parent);
		long newId = this.sInfoCommentDao.save(s);
		if(newId>0){
			return true;
		}
		return false;
		//s.setId(newId);
		//if(newId>0){
		/*List<SInfoComment> list = parent.getSinfoComments();
		if(!list.add(s)){
			return false;
		}
		parent.setSinfoComments(list);
		this.sInfoCommentDao.update(parent);*/
		//}else{			
			//return false;
		//}
	}
	@Override
	public boolean addSInfoComment(int sinfoId, String replyContent,
			Customer user) {
		// TODO Auto-generated method stub
		SInfoComment sic = new SInfoComment();
		sic.setContent(replyContent);
		sic.setCustomer(user);
		sic.setPublishTime(new Date());
		
		SInfo sinfo = sInfoDao.get(sinfoId);
		sinfo.setReply((sinfo.getReply()+1));
		this.sInfoDao.update(sinfo);
		sic.setSinfo(sinfo);
		long id = sInfoCommentDao.save(sic);
		if(id>0){
			return true;
		}else{			
			return false;
		}
	}
	@Override
	public List<SInfoComment> getSInfoCommentBySInfoId(int sinfoId,int page,int pageSize) {
		if(sinfoId<=0){
			return null;
		}
		String queryString = " from common.post.bean.SInfoComment s where s.sinfo.id="+sinfoId+ " order by s.id desc";
		
		return this.sInfoCommentDao.find(queryString, page, pageSize);
	}
	@Override
	public long getSInfoCommentCountBySInfoId(int sinfoId) {
		String queryString = " select count(s) from common.post.bean.SInfoComment s where s.sinfo.id="+sinfoId+ " ";
		
		return (Long)this.sInfoCommentDao.find(queryString, true).get(0);
	}
	//===============getter and se3tter method
	public SInfoCommentDao getsInfoCommentDao() {
		return sInfoCommentDao;
	}
	public void setsInfoCommentDao(SInfoCommentDao sInfoCommentDao) {
		this.sInfoCommentDao = sInfoCommentDao;
	}
	public SInfoDao getsInfoDao() {
		return sInfoDao;
	}
	public void setsInfoDao(SInfoDao sInfoDao) {
		this.sInfoDao = sInfoDao;
	}
}
